package com.zz.cc.business.view;



import com.zz.cc.business.R;
import com.zz.common.tools.WeakReferenceHandler;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;


public class ActionSheet extends Dialog implements Callback {
	
	private static final String TAG = "ActionSheet";
	
	private Context mContext;
	private LayoutInflater mInflater;
	private Resources mResources;
		
	private WeakReferenceHandler mHandler;
	private TranslateAnimation animation;
	
	/**
	 * actionsheet根View
	 */
	private ScrollView mRootView;
	
	/**
	 * 下方actionview
	 */
	private LinearLayout mActionView;
	
	/**
	 * 头部view
	 */
	private LinearLayout mTitleContainer;
	
	/**
	 * 内容view
	 */
	private LinearLayout mContentContainer;
	
	
	/**
	 * title部分View
	 */
	private LinearLayout mTitleView = null;
	
	private int mButtonCount;
	
    
    /**
     * action Button选中时的监听器
     */
    private OnButtonClickListener mOnButtonsListener = null;
    
	private OnDismissListener mOnDismissListener = null;
	
	public interface OnDismissListener {
		public void onDismiss();
	}

    public interface OnButtonClickListener {
        public void OnClick(View clickedView, int which);
    }
    
    
	public static ActionSheet create(Context context) {
		final ActionSheet dialog = new ActionSheet(context);
		dialog.getWindow().setWindowAnimations(R.style.MenuDialogAnimation);
		return dialog;
	}

	/**
	 * 构造函数
	 * @param context
	 * @param checkMode 单选模式标识一个按钮被选定
	 */
	protected ActionSheet(Context context) {
		super(context, R.style.MenuDialogStyle);
		
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mResources = context.getResources();
		mHandler = new WeakReferenceHandler(Looper.getMainLooper(), this);
			
		mRootView = (ScrollView) mInflater.inflate(R.layout.action_sheet_base, null);
		mActionView = (LinearLayout) mRootView.findViewById(R.id.action_sheet_actionView);
		mTitleContainer = (LinearLayout) mRootView.findViewById(R.id.action_sheet_titleContainer);

		mContentContainer = (LinearLayout) mRootView
				.findViewById(R.id.action_sheet_contentView);
		super.setContentView(mRootView);
	}
		
	/**
	 * 对话框按钮点击事件监听
	 */
	private View.OnClickListener mHandleBtnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			
			int id = v.getId();
			
			/**
			 * 回调click事件
			 */
			if (mOnButtonsListener != null) {
				mOnButtonsListener.OnClick(v, id);
			}
		}
	};
	
	/**
	 * 默认的关闭对话框监听器
	 */
	private View.OnClickListener mHandleDismissListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			dismiss();
			if (mOnDismissListener != null) {
				mOnDismissListener.onDismiss();
			}
		}
	};

    
    public void setOnButtonClickListener(OnButtonClickListener l) {
    	mOnButtonsListener = l;
    }
    
    public void setOnDismissListener(OnDismissListener lis) {
    	mOnDismissListener = lis;
    }
    
	/**
	 * 原Dialog方法,已无作用
	 * 
	 * @deprecated
	 */
	@Override
	public void setContentView(View view) {
		throw new UnsupportedOperationException("this method is not support");
	}

	@Override
	public void setContentView(int layoutResID) {
		throw new UnsupportedOperationException("this method is not support");
	}

	@Override
	public void setTitle(int resId) {
		throw new UnsupportedOperationException("this method is not support");
	}

	@Override
	public void setTitle(CharSequence title) {
		throw new UnsupportedOperationException("this method is not support");
	}
	
	public void setMainTitle(int resid) {
		CharSequence text = mResources.getText(resid);
		setMainTitle(text);
	}
	
	public void setMainTitle(CharSequence text) {
		if (text != null && text.length() != 0) {
			if (mTitleView == null) {				
				mTitleView = (LinearLayout)mInflater.inflate(R.layout.action_sheet_title, null);
				mTitleContainer.addView(mTitleView, new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			
			TextView tilteView = (TextView)mTitleView.findViewById(R.id.action_sheet_title);
			tilteView.setVisibility(View.VISIBLE);
			tilteView.setText(text);
		}
	}
	
	public void setSecondaryTitle(int resid) {
		CharSequence text = mResources.getText(resid);
		setSecondaryTitle(text);
	}
	
	public void setSecondaryTitle(CharSequence text) {
		if (text != null && text.length() != 0) {
			if (mTitleView == null) {				
				mTitleView = (LinearLayout)mInflater.inflate(R.layout.action_sheet_title, null);
				mTitleContainer.addView(mTitleView, new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			
			TextView tilteView = (TextView)mTitleView.findViewById(R.id.action_sheet_secondary_title);
			tilteView.setVisibility(View.VISIBLE);
			tilteView.setText(text);
		}
	}
	
	public void addButton(int id) {
		CharSequence text = mResources.getText(id);
		addButton(text);
	}
	
	public void addButton(CharSequence text) {
		View btnView = mInflater.inflate(R.layout.action_sheet_normal_button, null);
		
		// 设置button文字和id
		Button btn = (Button)btnView.findViewById(R.id.action_sheet_button);
		btn.setText(text);
		btn.setId(mButtonCount);
		
		mContentContainer.addView(btnView, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		
		btn.setOnClickListener(mHandleBtnClickListener);
		
		mButtonCount++;
	}
	
	public void addCancelButton(int resid) {
		CharSequence text = mResources.getText(resid);
		addCancelButton(text);
	}
	
	public void addCancelButton(CharSequence text) {
		View cancelView = mInflater.inflate(R.layout.action_sheet_cancel_button, null);
		
		Button btnCancel = (Button)cancelView.findViewById(R.id.action_sheet_btnCancel);
		btnCancel.setText(text);
		btnCancel.setId(mButtonCount);
		
		mContentContainer.addView(cancelView, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		btnCancel.setOnClickListener(mHandleDismissListener);
		mButtonCount++;
	}
	
	@Override
	public void show() {
		super.show();
		
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				/**
				 * 播放View动画从下升起
				 */
                animation = new TranslateAnimation(0, 0,
                		mActionView.getHeight(), 0);
                animation.setFillEnabled(true);
                animation.setInterpolator(AnimationUtils.loadInterpolator(
                        mContext, android.R.anim.decelerate_interpolator));
                animation.setDuration(300);
                mActionView.startAnimation(animation);
			}
		}, 0);
		
	}
	
	@Override
	public void dismiss() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {      
                animation = new TranslateAnimation(0, 0, 0, mActionView.getHeight());
                animation.setInterpolator(AnimationUtils.loadInterpolator(
                        mContext, android.R.anim.decelerate_interpolator));
                animation.setDuration(200);
                animation.setFillAfter(true);
                mActionView.startAnimation(animation);
                
                
                animation.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationEnd(Animation animation) {
						ActionSheet.super.dismiss();
						
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationStart(Animation animation) {
					}
                	
                });
			}
		}, 0);
	}
	
	public void superDismiss() {
		try{
			super.dismiss();
		}catch(Exception e){
			e = null;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		return true;
	}
}
